package ui.finance;

import javax.swing.JDialog;

import tool.MyButton;
import tool.MyLabel;
import vo.AccountorVo;
import vo.UserVo;
import vo.Vo;
import blservice.finance.AccountorBLService;

import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JButton;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.rmi.RemoteException;

public class UpdateAccountDialog extends JDialog{
	private static final long serialVersionUID = -2060781136506112069L;
	AccountorManagementPanel father;
	AccountorBLService bl;
	UserVo user;
	private JTextField oldName;
	private JTextField newName;
	public UpdateAccountDialog(AccountorManagementPanel father,AccountorBLService bl,UserVo user,String oldname) {
		this.setContentPane(new JPanel() {

			private static final long serialVersionUID = -8067534092982934270L;

			@Override
			public void paint(Graphics g) {
				this.setBackground(new Color(0, 0, 0, 0));
				Image background = Toolkit.getDefaultToolkit().getImage(
						"Graphics/framebackground.jpg");
				g.drawImage(background, 0, 0, null);
				super.paint(g);
			}
		});

		this.father=father;
		this.bl=bl;
		this.user=user;
		this.setBounds(200, 200, 400, 250);
		getContentPane().setLayout(null);
		
		JLabel label = new MyLabel("旧账户名：");
		label.setBounds(70, 40, 90, 15);
		getContentPane().add(label);
		
		oldName = new JTextField();
		oldName.setEditable(false);
		oldName.setBounds(167, 38, 143, 25);
		getContentPane().add(oldName);
		oldName.setColumns(10);
		oldName.setText(oldname);
		
		JLabel label_1 = new MyLabel("新账户名：");
		label_1.setBounds(79, 95, 90, 15);
		getContentPane().add(label_1);
		
		newName = new JTextField();
		newName.setBounds(167, 93, 143, 25);
		getContentPane().add(newName);
		newName.setColumns(10);
		newName.setText(oldname);
		
		JButton confirm = new MyButton("Graphics/Button82-25.png");
		confirm.setText("确定");
		confirm.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				AccountorVo oldvo=new AccountorVo();
				oldvo.set名称(oldName.getText());
				oldvo.setOperator(user.getName());
				oldvo.setOperation("修改账户名");
				AccountorVo newvo=new AccountorVo();
				newvo.set名称(newName.getText());
				newvo.setOperator(user.getName());
				newvo.setOperation("修改账户名");
				Vo result;
				try {
					result = bl.updateAccountor(oldvo, newvo);
					if(result.isSuccessed()){
						JOptionPane.showMessageDialog(UpdateAccountDialog.this, "修改成功");
						father.showAll();
						UpdateAccountDialog.this.dispose();
					}else{
						JOptionPane.showMessageDialog(UpdateAccountDialog.this, "修改失败\n"+result.getFailMessage());
					}
				} catch (RemoteException e1) {
					JOptionPane.showMessageDialog(UpdateAccountDialog.this, "无法连接到服务器\n"+e1.getMessage());
					e1.printStackTrace();
				}
				
			}
		});
		confirm.setBounds(82, 156, 82, 25);
		getContentPane().add(confirm);
		
		JButton cancel = new MyButton("Graphics/Button82-25.png");
		cancel.setText("取消");
		cancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				UpdateAccountDialog.this.dispose();
			}
		});
		cancel.setBounds(215, 156, 82, 25);
		getContentPane().add(cancel);
		setTitle("修改名称");
		setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
	}
}